﻿<%@page import="com.zaofan.cache.CacheManage"%>
<%@page import="com.zaofan.service.CarrierAreaService"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="com.zaofan.bean.Admin"%>
<%@page import="com.zaofan.bean.AdminType"%>
<%@page import="com.zaofan.bean.OpenBuilder"%>
<%@page import="com.zaofan.bean.OpenArea"%>
<%@page import="com.zaofan.bean.CarrierArea"%>
<%@page import="java.util.List"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%
	String contextPath = request.getContextPath();
	Admin admin = (Admin)session.getAttribute("admin");
	if(admin == null){
	response.sendRedirect(contextPath + "/login.jsp?state=" + "session-invalid");
	return;
	}
	String carrierId = request.getParameter("carrierId");
	if(carrierId == null){
		out.print("非法访问!!");
		return;
	}
	Integer id = Integer.valueOf(carrierId);
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>运营商管理</title>
<link href="<%=contextPath%>/admin/css/base.css" rel="stylesheet" />
<link href="<%=contextPath%>/admin/assets/css/bootstrap.min.css"
	rel="stylesheet" />
<link rel="stylesheet"
	href="<%=contextPath%>/admin/assets/css/font-awesome.min.css" />
<!--[if IE 7]>
	<link rel="stylesheet" href="<%=contextPath%>/admin/assets/css/font-awesome-ie7.min.css" />
<![endif]-->
<link rel="stylesheet"
	href="<%=contextPath%>/admin/assets/css/ace.min.css" />
<link rel="stylesheet"
	href="<%=contextPath%>/admin/assets/css/ace-rtl.min.css" />
<link rel="stylesheet"
	href="<%=contextPath%>/admin/assets/css/ace-skins.min.css" />
<!--[if lte IE 8]>
	<link rel="stylesheet" href="<%=contextPath%>/admin/assets/css/ace-ie.min.css" />
<![endif]-->
<script src="<%=contextPath%>/admin/assets/js/ace-extra.min.js"></script>
<!--[if lt IE 9]>
	<script src="<%=contextPath%>/admin/assets/js/html5shiv.js"></script>
	<script src="<%=contextPath%>/admin/assets/js/respond.min.js"></script>
<![endif]-->
<link rel="stylesheet"
	href="<%=contextPath%>/admin/assets/css/jquery-ui-1.10.3.custom.min.css" />
<link rel="stylesheet"
	href="<%=contextPath%>/admin/assets/css/chosen.css" />
<script type="text/javascript">  
       var contextPath='<%=contextPath%>';
       var carrierId='<%=carrierId%>';
       
</script>
</head>
<body style="background-color: #fff;">
	<div class="breadcrumbs" id="breadcrumbs">
		<script type="text/javascript">
			try {
				ace.settings.check('breadcrumbs', 'fixed')
			} catch (e) {
			}
		</script>
		<ul class="breadcrumb" style="padding: 20px; color: #3a87ad;">
		</ul>
	</div>
	<div class="page-content">
		<div class="row" style="height: 100%">
			<div class="col-xs-12">
<a href="javascript:history.go(-1);" class="btn btn-primary" role="button">返回</a>
				<button type="button" class="btn btn-primary" id="addExpress">增加</button>
				<style>
.table-responsive table th tr {
	text-align: center;
}
</style>
				<div class="table-responsive">
					<input type="hidden" value="1" id="start" />
					<table id="sample-table-2"
						class="table table-striped table-bordered table-hover">
						<thead>
							<tr>
								<th style="text-align: center;">送餐员</th>
								<th style="text-align: center;">电话</th>
								<th style="text-align: center;">性别</th>
								<th style="text-align: center;">地址</th>
								<th style="text-align: center;">年龄</th>
								<th style="text-align: center;">状态</th>
								<th style="text-align: center;">操作</th>
							</tr>
						</thead>
						<tbody id="data-list" style="text-align: center;">
						</tbody>
					</table>
				</div>
			</div>
			<!-- /.col -->
		</div>
		<!-- /.row -->
		<div class="row">
			<div class="col-sm-6">
				<div id="sample-table-2_info" class="dataTables_info">
					当前 <span id="current"> </span> 到 <span id="page"></span> 总共 <span
						id="total"></span> 条
				</div>
			</div>
			<div class="col-sm-6">
				<div class="dataTables_paginate paging_bootstrap">
					<ul class="pagination pagination-sm" id="page-list">
					</ul>
				</div>
			</div>
		</div>
	</div>
	<div id="loading"
		style="width: 32px; z-index: 999999; height: 32px; position: fixed; left: 40%; top: 40%; display: none;">
		<img src="<%=contextPath%>/admin/assets/css/images/loading.gif"
			width="32" height="32" />
	</div>
	<div class="popover"
		style="z-index: 9999; width: 150px; position: fixed; left: 40%; top: 40%;">
		<div class="arrow"></div>
		<div class="popover-inner">
			<h3 class="popover-title">操作提示</h3>
			<div class="popover-content" id="popover-content">
				<p>操作成功</p>
			</div>
		</div>
	</div>

	<div id="expressBox"
		style="display: none; z-index: 99; width: 600px; position: fixed; left: 20%; top: 10%; border: 1px solid #ccc; border-radius: 4px !important;">
		<div class="arrow"></div>
		<div class="popover-inner" style="">
			<h3 class="popover-title" id="">送餐员增加</h3>
			<div class="popover-content" id="" style="background-color: #F5F5F5;">
				<form class="form-horizontal" style="padding-top: 10px;"
					method="post" id="expressForm">
					<input type="hidden" value="add" id="action" /> <input
						type="hidden" name="id" id="id" /><input
						type="hidden" name="carrierId" value="<%=carrierId %>" />
					<div class="form-group">
						<label for="" class="col-sm-2 control-label">送餐员名称：</label>
						<div class="col-sm-9">
							<input class="form-control" name="name" id="name"></input>
						</div>
						<span class="input_tip" style="line-height: 30px;">*</span>
					</div>
					<div class="form-group">
						<label for="" class="col-sm-2 control-label">地址：</label>
						<div class="col-sm-9">
							<input class="form-control" name="address" id="address"></input>
						</div>
						<span class="input_tip" style="line-height: 30px;">*</span>
					</div>
					<div class="form-group">
						<label for="" class="col-sm-2 control-label">联系电话：</label>
						<div class="col-sm-9">
							<input class="form-control" name="phone" id="phone"></input>
						</div>
						<span class="input_tip" style="line-height: 30px;">*</span>
					</div>
					<div class="form-group">
						<label for="" class="col-sm-2 control-label">性别：</label>
						<div class="col-sm-9">
							<input type="radio" checked="checked" name="sex" value="男" />男
							<input type="radio" name="sex" value="女" />女
						</div>
						<span class="input_tip" style="line-height: 30px;">*</span>
					</div>
					<div class="form-group">
						<label for="" class="col-sm-2 control-label">年龄：</label>
						<div class="col-sm-9">
							<input class="form-control" name="age" id="age"></input>
						</div>
						<span class="input_tip" style="line-height: 30px;">*</span>
					</div>
				</form>
			</div>
			<div class="popover-footer"
				style="background-color: #F5F5F5; overflow: hidden; text-align: center;">
				<button type="button" class="btn btn-primary"
					style="padding: 0px; margin: 0px 5px 5px 0px;" id="cancel">
					<i class="icon-ok"></i>取消
				</button>
				<button type="button" class="btn btn-danger"
					style="padding: 0px; margin: 0px 5px 5px 0px;" id="save">
					<i class="icon-edit"></i>保存
				</button>

			</div>
		</div>
	</div>


	<div id="areasBox"
		style="display: none; z-index: 99; width: 600px; position: fixed; left: 20%; top: 10%; border: 1px solid #ccc; border-radius: 4px !important;">
		<div class="arrow"></div>
		<div class="popover-inner" style="">
			<h3 class="popover-title" id="">大楼分配</h3>
			<div class="popover-content" id="" style="background-color: #F5F5F5;">
				<form class="form-horizontal" style="padding-top: 10px;"
					method="post" id="areaForm">
					<input type="hidden" name="id" id="id1" />
					<div class="form-group">
						<div class="col-sm-9">
							<%
								CarrierAreaService carrierAreaService = new CarrierAreaService();
								List<CarrierArea> areas = carrierAreaService.findByCarrierId(id);
								if(areas!=null&& areas.size() > 0){
									for(int i = 0 ; i < areas.size() ; i++){
										List<OpenBuilder> builders = CacheManage.openBuilders.get(areas.get(i).getAreaId());
										if(builders!=null && builders.size() > 0){
											
											%>
											<%=builders.get(0).getAreaName() %>:<br/>
											<%
											
											for(int j = 0 ; j < builders.size() ; j++){
												%>
												<input type="checkbox" value="<%=builders.get(j).getId() %>" name="builderIds" /><%=builders.get(j).getBuilderName() %>							
												<%
												if((j + 1) %3 == 0){
													%>
													<br/>
												<%
												}
											}
										}
										%>
											<br/><br/>
										<%
									}
								}
							%>
						</div>
					</div>
				</form>
			</div>
			<div class="popover-footer"
				style="background-color: #F5F5F5; overflow: hidden; text-align: center;">
				<button type="button" class="btn btn-primary"
					style="padding: 0px; margin: 0px 5px 5px 0px;" id="cancel1">
					<i class="icon-ok"></i>取消
				</button>
				<button type="button" class="btn btn-danger"
					style="padding: 0px; margin: 0px 5px 5px 0px;" id="save1">
					<i class="icon-edit"></i>保存
				</button>

			</div>
		</div>
	</div>


	<script type="text/javascript">
		if ("ontouchend" in document)
			document
					.write("<script src='<%=contextPath%>/admin/assets/js/jquery.mobile.custom.min.js'>"
							+ "<"+"/script>");
	</script>
	<script type="text/javascript">
		window.jQuery
				|| document
						.write("<script src='<%=contextPath%>/admin/assets/js/jquery-2.0.3.min.js'>"
								+ "<"+"script>");
	</script>

	<!--[if IE]>
		<script type="text/javascript">
			window.jQuery || document.write("<script src='<%=contextPath%>/admin/assets/js/jquery-1.10.2.min.js'>"+"<"+"script>");
		</script>
	<![endif]-->
	<script type="text/javascript">
		if ("ontouchend" in document)
			document
					.write("<script src='<%=contextPath%>/admin/assets/js/jquery.mobile.custom.min.js'>"
							+ "<"+"script>");
	</script>
	<script src="<%=contextPath%>/admin/assets/js/bootstrap.min.js"></script>
	<script src="<%=contextPath%>/admin/assets/js/typeahead-bs2.min.js"></script>

	<!--[if lte IE 8]>
		  <script src="assets/js/excanvas.min.js"></script>
	<![endif]-->
	<script
		src="<%=contextPath%>/admin/assets/js/jquery-ui-1.10.3.custom.min.js"></script>
	<script
		src="<%=contextPath%>/admin/assets/js/jquery.ui.touch-punch.min.js"></script>
	<script src="<%=contextPath%>/admin/assets/js/jquery.slimscroll.min.js"></script>
	<script
		src="<%=contextPath%>/admin/assets/js/jquery.easy-pie-chart.min.js"></script>
	<script src="<%=contextPath%>/admin/assets/js/jquery.sparkline.min.js"></script>
	<script src="<%=contextPath%>/admin/assets/js/ace-elements.min.js"></script>
	<script src="<%=contextPath%>/admin/assets/js/ace.min.js"></script>
	<script src="<%=contextPath%>/admin/js/common.js"></script>
	<script src="<%=contextPath%>/admin/assets/js/jquery-form.js"></script>
	<script
		src="<%=contextPath%>/admin/assets/js/jquery.dataTables.bootstrap.js"></script>
	<script
		src="<%=contextPath%>/admin/assets/js/jquery.dataTables.bootstrap.js"></script>
	<script
		src="<%=contextPath%>/admin/assets/js/date-time/bootstrap-datepicker.min.js"></script>


	<script type="text/javascript">
		$(function() {
			init();
		});
		function init() {
			getData(1);
		}

		function getData(start) {
			$("#loading").show();
			$.ajax({
				url : contextPath + '/express',
				type : "POST",
				dataType : 'json',
				data : {
					action : 'list',
					carrierId : carrierId,
					begin : (start - 1) * pageSize,
					end : pageSize,
					order : " by addTime desc"
				},
				success : function(rs, textStatus, jqXHR) {
					if (rs.success) {
						var data = rs.data;
						var total = rs.total;
						var dataList = $("#data-list");
						dataList.empty();
						$("#total").text(total);
						$("#current").text((start - 1) * pageSize + 1);
						if (data.length > 0) {
							$("#page").text(
									(start - 1) * pageSize + data.length);
							parseData(data, total, start);
						} else {
							showMessage("无数据");
						}
					} else {
						showMessage("获取数据失败,请稍后再试");
					}
					$("#loading").hide();

				},
				error : function() {
					$("#loading").hide();
					showMessage("请稍后再试");
				}
			});
		}

		function parseData(data, total, start) {
			var dataList = $("#data-list");
			var tr = "";
			for (var i = 0; i < data.length; i++) {
				tr += '<tr>';
				tr += '<td>' + data[i].name + '</td>';
				tr += '<td>' + data[i].phone + '</td>';
				tr += '<td>' + data[i].sex + '</td>';
				tr += '<td>' + data[i].address + '</td>';
				tr += '<td>' + data[i].age + '</td>';
				if (data[i].state == 0) {
					tr += '<td><span class="label label-success">再用</span></td>';
					tr += '<td><input type="hidden" value="'+data[i].id+'" class="expressId"/><a class="edit">编辑</a> <a class="state">禁用</a>&nbsp;<a class="builders">大楼分配</a></td>';
				} else {
					tr += '<td><span class="label label-danger">停用</span></td>';
					tr += '<td><input type="hidden" value="'+data[i].id+'" class="expressId"/><a class="edit">编辑</a> <a class="state">启用</a>&nbsp;<a class="builders">大楼分配</a></td>';
				}
				tr += '</tr>';
			}
			dataList.append(tr);

			$(".edit").bind("click", function() {
				var id = $(this).parent().find("input.expressId").val();
				$("#loading").show();
				$.ajax({
					url : contextPath + '/express',
					type : "POST",
					dataType : 'json',
					data : {
						action : 'get',
						id : id
					},
					success : function(rs, textStatus, jqXHR) {
						if (rs.success) {
							var data = rs.data;
							$("#action").val("update");
							$("#expressBox").show();
							$("#id").val(data.id);
							$("#name").val(data.name);
							$("#phone").val(data.phone);
							$("#address").val(data.address);
							$("#age").val(data.age);
							$("input[name='sex'][value='"+data.sex+"']").prop("checked","checked");
						} else {
							showMessage(rs.msg);
						}
						$("#loading").hide();
					},
					error : function() {
						$("#loading").hide();
						showMessage("请稍后再试");
					}
				});
			});

			
			$(".builders").bind("click", function() {
				if($("input[type='checkbox'][name='builderIds']").length == 0){
					showMessage("请先设置运营商的区域信息");
					return;
				}
				var id = $(this).parent().find("input.expressId").val();
				$("#id1").val(id);	
				$("#loading").show();
				$.ajax({
					url : contextPath + '/express',
					type : "POST",
					dataType : 'json',
					data : {
						action : 'builders_list',
						id:id
					},
					success : function(rs, textStatus, jqXHR) {
						if (rs.success) {
							var data = rs.data;
							$("#areasBox").show();
							$("input[type='checkbox'][name='builderIds']").prop("checked","");
							for(var i  = 0 ; i < data.length ; i++){
								$("input[type='checkbox'][name='builderIds'][value='"+data[i].builderId+"']").prop("checked","checked");
							}		
							
						} else{
							showMessage(rs.msg);
						}
						$("#loading").hide();
					},
					error : function() {
						$("#loading").hide();
						showMessage("请稍后再试");
					}
				});
			});
			
			$(".state").bind("click", function() {
				var id = $(this).parent().find("input.expressId").val();
				$("#loading").show();
				$.ajax({
					url : contextPath + '/express',
					type : "POST",
					dataType : 'json',
					data : {
						action : 'state',
						id : id
					},
					success : function(rs, textStatus, jqXHR) {
						if (rs.success) {
							showMessage(rs.msg);
							setTimeout(function() {
								getData(1);
							}, 1000);
						} else {
							showMessage(rs.msg);
						}
						$("#loading").hide();
					},
					error : function() {
						$("#loading").hide();
						showMessage("请稍后再试");
					}
				});
			});

			//分页
			var pageList = $("#page-list");
			pageList.empty();
			pageList.append(getPageInfo(start, total));
			$("html,body").animate({
				scrollTop : 0
			}, 200);
		}

		$("#addExpress").click(function() {
			$("#expressBox").show();
			$("#action").val("add");
			$("#id").val("");
			$("#name").val("");
			$("#phone").val("");
			$("#address").val("");
			$("#sex").val("");
			$("input[name='sex'][value='男']").prop("checked","checked");
		});

		$("#cancel").click(function() {
			$("#expressBox").hide();
			$("#action").val("add");
			$("#id").val("");
			$("#name").val("");
			$("#phone").val("");
			$("#address").val("");
			$("#sex").val("");
			$("input[name='sex'][value='男']").prop("checked","checked");
		});

		$("#save").click(function() {
			if ($("#name").val() == "") {
				showMessage("请输入运营商名称!!!");
				return;
			}
			if ($("#address").val() == "") {
				showMessage("请输入运营商地址!!!");
				return;
			}
			if ($("#phone").val() == "") {
				showMessage("请输入运营商联系电话!!!");
				return;
			}
			if ($("#sex").val() == "") {
				showMessage("请输入运营商规模!!!");
				return;
			}
			$("#loading").show();
			$("#expressForm").ajaxSubmit({
				type : "POST",
				url : contextPath + '/express?action=' + $("#action").val(),
				dataType : 'json',
				success : function(data) {
					if (data.success) {
						showMessage(data.msg);
						$("#expressBox").hide();
						$("#action").val("add");
						$("#id").val("");
						$("#name").val("");
						$("#phone").val("");
						$("#address").val("");
						$("#sex").val("");
						$("input[name='sex'][value='男']").prop("checked","checked");
						setTimeout(function() {
							getData(1);
						}, 2000);
					} else {
						showMessage(data.msg);
					}
					$("#loading").hide();
				},
				error : function(msg) {
					$("#loading").hide();
					showMessage("操作失败");
				}
			});
		});
		
		$("#save1").click(function(){
			var builderIds = "";
			$("input[type='checkbox'][name='builderIds']:checked").each(function(){
				builderIds += $(this).val() + ",";
			});
			$.ajax({
				url : contextPath + '/express',
				type : "POST",
				dataType : 'json',
				data : {
					action : 'builders',
					builderIds : builderIds,
					id : $("#id1").val()
				},
				success : function(rs, textStatus, jqXHR) {
					if (rs.success) {
						showMessage("操作成功");
						$("input[type='checkbox'][name='builderIds']").prop("checked","");
						$("#areasBox").hide();
					} else {
						showMessage(rs.msg);
					}
					$("#loading").hide();

				},
				error : function() {
					$("#loading").hide();
					showMessage("请稍后再试");
				}
			});
		});
		$("#cancel1").click(function(){
			$("input[type='checkbox'][name='builderIds']").prop("checked","");
			$("#areasBox").hide();
		});
		
	</script>
</body>
</html>